﻿using Autofac;
using Super.Hmi.Commands;
using Super.Hmi.Modules;
using Super.Hmi.Views;
using System.Configuration;
using System.Data;
using System.Windows;

namespace Super.Hmi;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    public IContainer Container { get; private set; }
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        var builder = new ContainerBuilder();                                                         // 创建Autofac容器

        builder.RegisterModule<ViewModule>();                                                         // 注册 View 模块
        builder.RegisterModule<ViewModelModule>();                                                    // 注册 ViewModel 模块
        builder.RegisterModule<DataBaseModule>();                                                     // 注册 DataBase 模块
        builder.RegisterModule<ServiceModule>();                                                      // 注册 Service 模块

        //builder.RegisterType<MessageBoxService>().As<IMessageBoxService>().InstancePerLifetimeScope();// 注册 MessageBox 弹窗模块
        //builder.RegisterType<DialogService>().As<IDialogService>().InstancePerDependency();           // 注册 IDialogService 弹窗模块
        //builder.RegisterType<OverlayService>().As<IOverlayService>().SingleInstance();         // 注册 IOverlayService 蒙版模块
        //builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope();            // 注册 UserService 模块
        //builder.RegisterType<PermissionService>().As<IPermissionService>().InstancePerLifetimeScope();            // 注册 MneuService 模块
        //builder.RegisterType<IconService>().As<IIconService>().InstancePerLifetimeScope();            // 注册 IconService 模块
        //builder.RegisterType<RoleService>().As<IRoleService>().InstancePerLifetimeScope();            // 注册 IRoleService 模块

        //builder.RegisterType<ThemeSettingHelper>().AsSelf().InstancePerLifetimeScope();               // 注册 ThemeSettingHelper
        //builder.RegisterType<ThemeConfig>().AsSelf().InstancePerLifetimeScope();                      // 注册 ThemeConfig

        builder.RegisterType<RelayCommand>().AsSelf();                                                 // 注册绑定
        Container = builder.Build();
        Container.Resolve<MainView>()?.Show();                                                        // 解析并显示主窗口
    }

    /// <summary>
    /// 关闭应用程序时释放资源
    /// </summary>
    /// <param name="e"></param>
    protected override void OnExit(ExitEventArgs e)
    {
        // 释放容器资源
        Container.Dispose();
        // 清理所有订阅
        Global.AppContext.Instance.EventTokenList.ForEach(t => t.Dispose());
        base.OnExit(e);
    }
}

